package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.util.UploadUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

//操作图片资源的控制器
@RestController
@RequestMapping("/images")
public class ImageController {

    /*
    上传图片
    资源:/images
    动作:POST
    参数:pic=二进制
     */
    @PostMapping
    public Object upload(@RequestParam MultipartFile pic) {
        Map<String, Object> map = new HashMap<>();
        //成功{status: "1", url: "xxx"} 失败{status: "0", msg:"xxx"}
        if (pic.getContentType().startsWith("image") && pic.getSize() > 0) {
            //把上传的图片保存到本地图库
            String imgUrl = UploadUtil.upload(pic);
            map.put("status", 1);
            map.put("url", imgUrl);
        } else {
            map.put("status", 0);
            map.put("msg", "请正确的选择图片");
        }
        return map;
    }
}
